﻿using System.Windows.Controls;

namespace CommonUtils
{
    public static class TextBoxExtensions
    {
        /// <summary>
        /// 空判断
        /// </summary>
        public static bool IsNullOrEmpty(this TextBox textBox)
        => string.IsNullOrEmpty(textBox.Text);

        /// <summary>
        /// 空判断
        /// </summary>
        public static bool IsNullOrWhiteSpace(this TextBox textBox)
        => string.IsNullOrWhiteSpace(textBox.Text);

        /// <summary>
        /// 是否为有效文件路径
        /// </summary>
        public static bool IsFilePath(this TextBox textBox)
        => FileUtil.Exists(textBox.Text);

        /// <summary>
        /// 获取文件夹路径,末尾不带斜杠"\\"
        /// </summary>
        public static void SelectFolder(this TextBox textBox, bool save = false)
        {
            string path = WindowsUtil.SelectFolder();
            if (!string.IsNullOrWhiteSpace(path))
            {
                textBox.Text = path;
                if (save)
                    textBox.SaveValue();
            }
        }

        public static void SelectFolderOnDoubleClick(this TextBox textBox, bool save = false)
        {
            textBox.MouseDoubleClick += delegate
            {
                textBox.SelectFolder(save);
            };
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        public static void SelectFile(this TextBox textBox, bool save = true, params string[] namesOrExtentions)
        {
            string path = WindowsUtil.SelectFile(namesOrExtentions);
            if (!string.IsNullOrWhiteSpace(path))
                textBox.Text = path;
            if (save)
                textBox.SaveValue();
        }

        /// <summary>
        /// 赋值
        /// </summary>
        public static void SetText(this TextBox textBox, string text)
        => textBox.Text = text;

        /// <summary>
        /// 限制长度
        /// </summary>
        public static void LengthLimit(this TextBox textBox, int length = 65535)
        {
            if (textBox.Text.Length > length)
                textBox.Text = textBox.Text.Substring(0, length / 2);
        }


        /// <summary>
        /// 附值
        /// </summary>
        public static void Append(this TextBox textBox, string text, int lengthLimit = 65535)
        {
            textBox.Dispatcher.Invoke(delegate
            {
                textBox.AppendText(text);
                textBox.LengthLimit(lengthLimit);
            });
        }

        /// <summary>
        /// 插值
        /// </summary>
        public static void Insert(this TextBox textBox, string text, int lengthLimit = 65535)
        {
            textBox.Dispatcher.Invoke(delegate
            {
                textBox.Text = text + textBox.Text;
                textBox.LengthLimit(lengthLimit);
            });
        }

        /// <summary>
        /// 以log形式输出
        /// </summary>
        public static void LogInsert(this TextBox textBox, object text, params object[] args)
        => textBox.Insert(LogUtil.Format(text, args));

        /// <summary>
        /// 以log形式输出
        /// </summary>
        public static void LogAppend(this TextBox textBox, object text, params object[] args)
         => textBox.Append(LogUtil.Format(text, args));

        /// <summary>
        /// 以log形式输出
        /// </summary>
        public static void Log(this TextBox textBox, object text, params object[] args)
        => textBox.LogInsert(text, args);
    }
}
